package CalendarComponents;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import Managers.CategoryManager;

@SuppressWarnings("serial")
public class CategoryDialog extends JDialog {
	JButton bConfirm;
	JButton bCancel;
	JTextField newName;
	JDialog dialogFrame;
	Color colour;
	JButton btnColour;

	public CategoryDialog(JFrame parent) {
		super(parent, "Category Manager", true);
		if (parent != null) {
			Dimension parentSize = parent.getSize();
			Point p = parent.getLocation();
			setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
		}
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setResizable(false);
		this.dialogFrame = this;

		JLabel label = new JLabel("New Category: ");
		newName = new JTextField("Name Here");
		newName.setPreferredSize(new Dimension(200,
				newName.getPreferredSize().height));

		btnColour = new JButton("Select Colour");
		btnColour.addActionListener(new ColourSelect());

		ButtonListener bl = new ButtonListener();
		bConfirm = new JButton("OK");
		bCancel = new JButton("Cancel");
		bConfirm.addActionListener(bl);
		bCancel.addActionListener(bl);

		JPanel mainPanel = new JPanel();
		JPanel topPanel = new JPanel();
		JPanel bottomPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(2, 1));

		topPanel.add(label);
		topPanel.add(newName);
		topPanel.add(btnColour);
		mainPanel.add(topPanel);

		bottomPanel.add(bConfirm);
		bottomPanel.add(bCancel);
		mainPanel.add(bottomPanel);

		add(mainPanel);
		pack();
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(bConfirm)) {
				CategoryManager.getInstance();
				// MAKE NEW CATEGORY WITH NAME OF: newName.getText();
				// And use colour, if user has selected one.
				CategoryManager.addCategory(newName.getText(),
						colour);
				dialogFrame.dispose();
			} else if (e.getSource().equals(bCancel)) {
				dialogFrame.dispose();
			}
		}
	}

	private class ColourSelect implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			colour = JColorChooser.showDialog(dialogFrame,
					"Select category colour.", Color.white);
			// Provide some feedback?
			btnColour.setBackground(colour);
		}
	}
}
